function alpha_pos = gwo_optimization(final_net, X_train, Y_train, num_iterations, num_wolves)  
    % Initialize the positions of wolves randomly  
    dim = numel(getwb(final_net)); % Number of weights  
    positions = rand(num_wolves, dim); % Randomly initialized positions of wolves  
    fitness_values = zeros(num_wolves, 1); % To store fitness values  

    % Ensure Y_train is a column vector for consistent comparison  
    Y_train = Y_train(:); % Convert Y_train to a column vector  

    for iter = 1:num_iterations  
        % Evaluate fitness of each wolf  
        for i = 1:num_wolves  
            % Set the weights to the network from the position  
            final_net = setwb(final_net, positions(i, :)');  

            % Calculate the output and loss (Mean Squared Error as an example)  
            Y_pred = final_net(X_train'); % Transpose X_train for prediction  

            % Convert Y_pred to a column vector  
            Y_pred = Y_pred(:);  

            % Display sizes for debugging  
            disp('-----------------------------------');  
            disp(['Iteration ', num2str(iter), ', Wolf ', num2str(i)]);  
            disp(['Size of Y_train: ', num2str(size(Y_train))]);  
            disp(['Size of Y_pred: ', num2str(size(Y_pred))]);  % Debugging output  

            % Check if the sizes match before calculating MSE  
            if size(Y_train) == size(Y_pred)  
                fitness_values(i) = mean((Y_train - Y_pred).^2); % Calculate fitness  
            else  
                disp('Size mismatch detected!');  
                disp(['Y_train size: ', num2str(size(Y_train))]);  
                disp(['Y_pred size: ', num2str(size(Y_pred))]);  
                error('Size mismatch between Y_train and Y_pred!');  % Stop on mismatch  
            end  
        end  

        % Sort wolves by fitness and update alpha (best) position  
        [~, sorted_indices] = sort(fitness_values);  
        best_wolf = positions(sorted_indices(1), :);  

        % Update positions (This is a simplification of GWO)  
        a = 2 - iter * (2 / num_iterations); % Decreasing factor  
        for i = 1:num_wolves  
            for j = 1:dim  
                r = rand(); % Random number in [0,1]  
                % Update position based on best wolf (simplified)  
                positions(i, j) = best_wolf(j) + r * a * (best_wolf(j) - positions(i, j));  
            end  
        end  
    end  

    alpha_pos = best_wolf; % Return the best position found  
end  
